package com.ruoyi.reportTask.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.reportTask.domain.IndustrialReportTask;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportTask.domain.bo.IndustrialReportTaskCenterBo;
import com.ruoyi.reportTask.domain.vo.IndustrialReportTaskCenterVo;
import com.ruoyi.reportTask.domain.IndustrialReportTaskCenter;
import com.ruoyi.reportTask.mapper.IndustrialReportTaskCenterMapper;
import com.ruoyi.reportTask.service.IIndustrialReportTaskCenterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报任务中心Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-18
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportTaskCenterServiceImpl implements IIndustrialReportTaskCenterService {

    private final IndustrialReportTaskCenterMapper baseMapper;

    /**
     * 查询填报任务中心
     */
    @Override
    public IndustrialReportTaskCenterVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报任务中心列表
     */
    @Override
    public TableDataInfo<IndustrialReportTaskCenterVo> queryPageList(IndustrialReportTaskCenterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportTaskCenter> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportTaskCenterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报任务中心列表
     */
    @Override
    public List<IndustrialReportTaskCenterVo> queryList(IndustrialReportTaskCenterBo bo) {
        LambdaQueryWrapper<IndustrialReportTaskCenter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportTaskCenter> buildQueryWrapper(IndustrialReportTaskCenterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportTaskCenter> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTaskUrl()), IndustrialReportTaskCenter::getTaskUrl, bo.getTaskUrl());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), IndustrialReportTaskCenter::getTaskName, bo.getTaskName());
        lqw.eq(bo.getGenDate() != null, IndustrialReportTaskCenter::getGenDate, bo.getGenDate());
        lqw.eq(bo.getDeadline() != null, IndustrialReportTaskCenter::getDeadline, bo.getDeadline());
        lqw.eq(StringUtils.isNotBlank(bo.getReportObj()), IndustrialReportTaskCenter::getReportObj, bo.getReportObj());
        lqw.eq(StringUtils.isNotBlank(bo.getReportCycle()), IndustrialReportTaskCenter::getReportCycle, bo.getReportCycle());
        lqw.eq(bo.getTaskStatus() != null, IndustrialReportTaskCenter::getTaskStatus, bo.getTaskStatus());
        lqw.eq(bo.getSendNum() != null, IndustrialReportTaskCenter::getSendNum, bo.getSendNum());

        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            IndustrialReportTaskCenter::getSendDate, params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增填报任务中心
     */
    @Override
    public Boolean insertByBo(IndustrialReportTaskCenterBo bo) {
        IndustrialReportTaskCenter add = BeanUtil.toBean(bo, IndustrialReportTaskCenter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报任务中心
     */
    @Override
    public Boolean updateByBo(IndustrialReportTaskCenterBo bo) {
        IndustrialReportTaskCenter update = BeanUtil.toBean(bo, IndustrialReportTaskCenter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportTaskCenter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填报任务中心
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
